package com.ruoyi.ams.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.ams.domain.vo.WxTreeSelect;
import com.ruoyi.ams.mapper.AmsAssetTypeMapper;
import com.ruoyi.ams.service.IAmsAssetTypeService;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.model.AmsAssetType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资产类型信息Service业务层处理
 * 
 * @author yfkj
 * @date 2021-10-15
 */
@Service
public class AmsAssetTypeServiceImpl extends ServiceImpl<AmsAssetTypeMapper, AmsAssetType>  implements IAmsAssetTypeService
{
    @Autowired
    private AmsAssetTypeMapper amsAssetTypeMapper;

    /**
     * 查询资产类型信息
     * 
     * @param assetTypeId 资产类型信息主键
     * @return 资产类型信息
     */
    @Override
    public AmsAssetType selectAmsAssetTypeByAssetTypeId(Long assetTypeId)
    {
        return amsAssetTypeMapper.selectAmsAssetTypeByAssetTypeId(assetTypeId);
    }

    /**
     * 查询资产类型信息列表
     * 
     * @param amsAssetType 资产类型信息
     * @return 资产类型信息
     */
    @Override
    public List<AmsAssetType> selectAmsAssetTypeList(AmsAssetType amsAssetType)
    {
        return amsAssetTypeMapper.selectAmsAssetTypeList(amsAssetType);
    }

    /**
     * 新增资产类型信息
     * 
     * @param amsAssetType 资产类型信息
     * @return 结果
     */
    @Override
    public int insertAmsAssetType(AmsAssetType amsAssetType)
    {
        amsAssetType.setCreateTime(System.currentTimeMillis());
        return amsAssetTypeMapper.insertAmsAssetType(amsAssetType);
    }

    /**
     * 修改资产类型信息
     * 
     * @param amsAssetType 资产类型信息
     * @return 结果
     */
    @Override
    public int updateAmsAssetType(AmsAssetType amsAssetType)
    {
        amsAssetType.setUpdateTime(System.currentTimeMillis());
        return amsAssetTypeMapper.updateAmsAssetType(amsAssetType);
    }

    /**
     * 批量删除资产类型信息
     * 
     * @param assetTypeIds 需要删除的资产类型信息主键
     * @return 结果
     */
    @Override
    public int deleteAmsAssetTypeByAssetTypeIds(Long[] assetTypeIds)
    {
        return amsAssetTypeMapper.deleteAmsAssetTypeByAssetTypeIds(assetTypeIds);
    }

    /**
     * 删除资产类型信息信息
     * 
     * @param assetTypeId 资产类型信息主键
     * @return 结果
     */
    @Override
    public int deleteAmsAssetTypeByAssetTypeId(Long assetTypeId)
    {
        return amsAssetTypeMapper.deleteAmsAssetTypeByAssetTypeId(assetTypeId);
    }
    @Override
    public List<TreeSelect> buildTreeSelect(List<AmsAssetType> types)
    {
        List<AmsAssetType> menuTrees = buildTree(types);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }
    @Override
    public List<WxTreeSelect> buildWxTreeSelect(List<AmsAssetType> list)
    {
        List<AmsAssetType> menuTrees = buildTree(list);
        return menuTrees.stream().map(WxTreeSelect::new).collect(Collectors.toList());
    }


    @Override
    public List<TreeSelect> buildTreeSelectExcel(List<AmsAssetType> types)
    {
        List<AmsAssetType> menuTrees = buildTree(types);
        return menuTrees.stream().map(menuTree -> new TreeSelect(menuTree, false)).collect(Collectors.toList());
    }

    public List<AmsAssetType> buildTree(List<AmsAssetType> types)
    {
        List<AmsAssetType> returnList = new ArrayList<AmsAssetType>();
        List<Long> tempList = new ArrayList<Long>();
        for (AmsAssetType type : types)
        {
            tempList.add(type.getAssetTypeId());
        }

        for (Iterator<AmsAssetType> iterator = types.iterator(); iterator.hasNext();)
        {
            AmsAssetType menu = (AmsAssetType) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId()))
            {
                recursionFn(types, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = types;
        }
        return returnList;
    }
    private void recursionFn(List<AmsAssetType> list, AmsAssetType t)
    {
        // 得到子节点列表
        List<AmsAssetType> childList = getChildList(list, t);
        t.setChildren(childList);
        int assetNum = t.getAssetNum()==null?0:t.getAssetNum();
        for (AmsAssetType tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
            int thisNUm =tChild.getAssetNum() == null ? 0 : tChild.getAssetNum();
            assetNum =assetNum+thisNUm ;
        }
        t.setAssetNum(assetNum);
    }
    /**
     * 得到子节点列表
     */
    private List<AmsAssetType> getChildList(List<AmsAssetType> list, AmsAssetType t)
    {
        List<AmsAssetType> tlist = new ArrayList<AmsAssetType>();
        Iterator<AmsAssetType> it = list.iterator();
        while (it.hasNext())
        {
            AmsAssetType n = (AmsAssetType) it.next();
            if (n.getParentId().longValue() == t.getAssetTypeId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<AmsAssetType> list, AmsAssetType t)
    {
        return getChildList(list, t).size() > 0;
    }
}
